package com.ktwlsoft.archivemanage.domain.business.businessService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.AdNotifyRepository;
import com.ktwlsoft.archivemanage.domain.dao.entity.Account;
import com.ktwlsoft.archivemanage.domain.dao.entity.AdNotify;
import com.ktwlsoft.archivemanage.payload.adNotify.AddAdNotifyRequest;
import com.ktwlsoft.archivemanage.payload.adNotify.EditAdNotifyRequest;
import com.ktwlsoft.archivemanage.payload.exception.BusinessException;
import com.ktwlsoft.archivemanage.service.security.UserPrincipal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class AdNotifyService {

    private Logger logger = LoggerFactory.getLogger(AdNotifyService.class);

    private final AdNotifyRepository adNotifyRepository;

    public AdNotifyService(AdNotifyRepository adNotifyRepository) {
        this.adNotifyRepository = adNotifyRepository;
    }

    public int save(AddAdNotifyRequest request) {
        AdNotify adNotify = new AdNotify(
                request.getTitle(),
                request.getSubTitle(),
                request.getImgUrl(),
                request.getContent(),
                request.getTag(),
                request.getShow()
        );
        Account account = UserPrincipal.getCurrentUser();
        if(account != null) {
            adNotify.setCreator(account.getRealName());
            adNotify.setCreatorId(account.getId());
            adNotify.setOperateTime(new Date());
            adNotify.setOperator(account.getRealName());
            adNotify.setOperatorId(account.getId());
        }
        if(request.getUp()){
            adNotify.setState(AdNotify.State.UP);
        }else{
            adNotify.setState(AdNotify.State.WAIT_UP);
        }
        int result = adNotifyRepository.getAdNotifyMapper().insert(adNotify);
        if (result <= 0) {
            logger.error("添加通知失败，数据库没有正确返回添加的行数。");
            throw new BusinessException("添加通知失败！");
        }
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    public AdNotify getById(Long id) {
        this.addReadCountByOne(id);
        return adNotifyRepository.getAdNotifyMapper().selectByPrimaryKey(id);
    }

    public AdNotify getByIdFromManager(Long id) {
        return adNotifyRepository.getAdNotifyMapper().selectByPrimaryKey(id);
    }

    public int deleteById(Long id) {
        return adNotifyRepository.getAdNotifyMapper().deleteByPrimaryKey(id);
    }

    public int upOrDownById(Long id, AdNotify.State state, Boolean shown) {
        Account account = UserPrincipal.getCurrentUser();
        return adNotifyRepository.getAdNotifyMapper().upOrDownById(id, state, new Date(), account.getRealName(), account.getId(),shown);
    }

    public PageInfo<?> findAll(Integer pn, Integer pageSize, AdNotify.State state, String operateFrom, String operateTo, String operatorId, String keywords) {
        PageHelper.startPage(pn,pageSize);
        return new PageInfo<>(adNotifyRepository.getAdNotifyMapper().selectAll(state, operateFrom, operateTo, operatorId, keywords));
    }

    /**
     * 子系统通知查看列表 on 2019/11/27 14:34
     *
     * @param state state
     * @param operateFrom operateFrom
     * @param operateTo operateTo
     * @param operatorId operatorId
     * @param keywords keywords
     * @return PageInfo<?>
     */
    public PageInfo<?> sonFindAll(Integer pn, Integer pageSize, AdNotify.State state, String operateFrom, String operateTo, String operatorId, String keywords) {
        PageHelper.startPage(pn,pageSize);
        return new PageInfo<>(adNotifyRepository.getAdNotifyMapper().sonSelectAll(state, operateFrom, operateTo, operatorId, keywords));
    }

    public int edit(EditAdNotifyRequest request) {
        if(request.getUp()){
            request.setState(AdNotify.State.UP);
        }else{
            request.setState(AdNotify.State.WAIT_UP);
        }
        Account account = UserPrincipal.getCurrentUser();
        request.setUpdater(account.getRealName());
        request.setUpdaterId(account.getId());
        return adNotifyRepository.getAdNotifyMapper().editAdNotify(request);
    }

    public int addReadCountByOne(Long id){
        return adNotifyRepository.getAdNotifyMapper().addReadCount(id);
    }
}
